package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.EventBusConfig;
import com.hemaapp.yjnh.bean.EventBusMsg;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.utils.RegexUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import xtom.frame.util.XtomToastUtil;

/**
 * 朋友代付
 *
 * @author ZhangGuiyou
 *         Date:2015-8-28 10:44
 */
public class FriendPayActivity extends BaseActivity {


    @Bind(R.id.title_left_btn)
    ImageButton titleLeftBtn;
    @Bind(R.id.title_right_btn)
    Button titleRightBtn;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.tv_goods_name)
    TextView tvGoodsName;
    @Bind(R.id.tv_fee)
    TextView tvFee;
    @Bind(R.id.tv_apply)
    TextView tvApply;
    @Bind(R.id.edtx_phone)
    EditText edtxPhone;
    @Bind(R.id.btn_ok)
    Button btnOk;

    private User user;
    private boolean dealAtHere = false;//是否在当前界面进行网络处理
    private String orderId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_friend_pay);
        ButterKnife.bind(this);
        user = getApplicationContext().getUser();
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getExras() {
        dealAtHere = mIntent.getBooleanExtra("deal_here", false);
        orderId = mIntent.getStringExtra("order_id");
    }

    @Override
    protected void findView() {
    }

    @Override
    protected void setListener() {
        titleText.setText("代付");
        titleRightBtn.setVisibility(View.GONE);

        String info = mIntent.getStringExtra("info");
        String goodsName = mIntent.getStringExtra("name");
        String fee = mIntent.getStringExtra("fee");
        tvGoodsName.setText(goodsName);
        tvFee.setText(fee);
        tvApply.setText(info);
    }

    @OnClick({R.id.title_left_btn, R.id.btn_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.btn_ok:
                String phone = edtxPhone.getText().toString();
                if (isNull(phone)) {
                    XtomToastUtil.showShortToast(mContext, "请输入代付人电话");
                    return;
                }
                if (!RegexUtils.judgeMobile(phone)) {
                    showTextDialog("电话号码输入不正确");
                    break;
                }
                if (phone.equals(user.getUsername())) {
                    XtomToastUtil.showShortToast(mContext, "不能找自己代付");
                    return;
                }
                if (dealAtHere) {
                    getNetWorker().orderSaveoperate(user.getToken(), orderId + "," + phone, "7", "无", "无", "无", "无");
                } else {
                    Intent itResult = new Intent();
                    itResult.putExtra("phone", phone);
                    setResult(RESULT_OK, itResult);
                    finish();
                }
                break;
        }
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        showProgressDialog("请稍候");
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        cancelProgressDialog();
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case ORDER_SAVEOPERATE:
                showTextDialog("操作成功，等待朋友代付");
                Intent itResult = new Intent();
                setResult(RESULT_OK, itResult);
                EventBusMsg msg = new EventBusMsg();
                msg.setType(EventBusConfig.UPDATE_ORDER);
                EventBus.getDefault().post(msg);
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        finish();
                    }
                }, 1000);
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case ORDER_SAVEOPERATE:
                XtomToastUtil.showShortToast(mContext, "操作失败，" + baseResult.getMsg());
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case ORDER_SAVEOPERATE:
                XtomToastUtil.showShortToast(mContext, "操作失败，请稍后再试");
                break;
        }
    }
}
